//
//  Scale9Sprite.m
//  jumpingfrog
//
//  Created by Tri Vo on 8/19/13.
//  Copyright 2013 Tri Vo. All rights reserved.
//

#import "Scale9Sprite.h"


@implementation Scale9Sprite

@synthesize opacity;
@synthesize color;

enum positions {
    pCentre = 0,
	pTop,
	pLeft,
	pRight,
	pBottom,
	pTopRight,
	pTopLeft,
	pBottomRight,
	pBottomLeft
};

CGSize baseSize;
CGRect resizableRegion;

- (id)initWithFile:(NSString *)file rect:(CGRect)rect centreRegion:(CGRect)centreRegion {
    if (self = [super init]) {
        _anchorPoint = ccp(0.5f, 0.5f);
        rect_ = rect;
        
        scale9Image = [[CCSpriteBatchNode alloc] initWithFile:file capacity:9];
        
        // Set up centre sprite
        centre = [[CCSprite alloc] initWithBatchNode:scale9Image rect:centreRegion];
        [scale9Image addChild:centre z:0 tag:pCentre];
        
        float l = rect_.origin.x;
        float t = rect_.origin.y;
        float h = rect_.size.height;
        float w = rect_.size.width;
        
        // Top
        top = [[CCSprite alloc] initWithBatchNode:scale9Image rect:CGRectMake(centreRegion.origin.x, t, centreRegion.size.width, centreRegion.origin.y - t)];
        [scale9Image addChild:top z:1 tag:pTop];
        top.color = ccc3(255, 0, 0);
        
        // Bottom
        bottom = [[CCSprite alloc] initWithBatchNode:scale9Image rect:CGRectMake(centreRegion.origin.x, centreRegion.origin.y + centreRegion.size.height, centreRegion.size.width, h - (centreRegion.origin.y - t + centreRegion.size.height))];
        
        [scale9Image addChild:bottom z:1 tag:pBottom];
        
        // Left
        left = [[CCSprite alloc] initWithBatchNode:scale9Image rect:CGRectMake(l, centreRegion.origin.y, centreRegion.origin.x - l, centreRegion.size.height)];
        [scale9Image addChild:left z:1 tag:pLeft];
        
        // right
        right = [[CCSprite alloc] initWithBatchNode:scale9Image rect:CGRectMake(centreRegion.origin.x + centreRegion.size.width, centreRegion.origin.y, w - (centreRegion.origin.x - l + centreRegion.size.width), centreRegion.size.height)];
        [scale9Image addChild:right z:1 tag:pRight];
        
        // Top left
        topLeft = [[CCSprite alloc] initWithBatchNode:scale9Image rect:CGRectMake(l, t, centreRegion.origin.x - l, centreRegion.origin.y - t)];
        [scale9Image addChild:topLeft z:2 tag:pTopLeft];
        
        // Top right
        topRight = [[CCSprite alloc] initWithBatchNode:scale9Image rect:CGRectMake(centreRegion.origin.x + centreRegion.size.width, t, w - (centreRegion.origin.x - l + centreRegion.size.width), centreRegion.origin.y - t)];
        [scale9Image addChild:topRight z:2 tag:pTopRight];
        
        // Bottom left
        bottomLeft = [[CCSprite alloc] initWithBatchNode:scale9Image rect:CGRectMake(l, centreRegion.origin.y + centreRegion.size.height, centreRegion.origin.x - l, h - (centreRegion.origin.y - t + centreRegion.size.height))];
        [scale9Image addChild:bottomLeft z:2 tag:pBottomLeft];
        
        // Bottom right
        bottomRight = [[CCSprite alloc] initWithBatchNode:scale9Image rect:CGRectMake(centreRegion.origin.x + centreRegion.size.width, centreRegion.origin.y  + centreRegion.size.height, w - (centreRegion.origin.x - l + centreRegion.size.width), h - (centreRegion.origin.y - t + centreRegion.size.height))];
        [scale9Image addChild:bottomRight z:2 tag:pBottomRight];
        
        baseSize = rect.size;
        resizableRegion = centreRegion;
        [self setContentSize:rect_.size];
        [self addChild:scale9Image];
        
    }
    return self;
}

- (void)dealloc {
    [topLeft release];
	[top release];
	[topRight release];
	[left release];
	[centre release];
	[right release];
	[bottomLeft release];
	[bottom release];
	[bottomRight release];
	[scale9Image release];
    [super dealloc];
}

- (void)setContentSize:(CGSize)contentSize {
    [super setContentSize:contentSize];
    
    
	float sizableWidth = contentSize.width - topLeft.contentSize.width - topRight.contentSize.width;
	float sizableHeight = contentSize.height - topLeft.contentSize.height - bottomRight.contentSize.height;
	float horizontalScale = sizableWidth/centre.contentSize.width;
	float verticalScale = sizableHeight/centre.contentSize.height;
    
    
    
    
	centre.scaleX = horizontalScale;
	centre.scaleY = verticalScale;
    
    
	float rescaledWidth = centre.contentSize.width * horizontalScale;
	float rescaledHeight = centre.contentSize.height * verticalScale;
    
	//Position corners
    //[self setAnchorPoint:CGPointMake(0.5f,0.5f)];
    
    float despx = contentSize.width*0.5f;
    float despy = contentSize.height*0.5f;
    
    
    
    //Position corners
    [topLeft setPosition:CGPointMake(-rescaledWidth/2 - topLeft.contentSize.width/2 +despx, rescaledHeight/2 + topLeft.contentSize.height*0.5 + despy) ];
    
    
    
    [topRight setPosition:CGPointMake(rescaledWidth/2 + topRight.contentSize.width/2 +despx, rescaledHeight/2 + topRight.contentSize.height*0.5 + despy)];
    [bottomLeft setPosition:CGPointMake(-rescaledWidth/2 - bottomLeft.contentSize.width/2 + despx, -rescaledHeight/2 - bottomLeft.contentSize.height*0.5 + despy)];
    [bottomRight setPosition:CGPointMake(rescaledWidth/2 + bottomRight.contentSize.width/2 + despx, -rescaledHeight/2 + -bottomRight.contentSize.height*0.5 + despy)];
    top.scaleX = horizontalScale;
    [top setPosition:CGPointMake(0+despx,rescaledHeight/2 + topLeft.contentSize.height*0.5 + despy)];
    bottom.scaleX = horizontalScale;
    [bottom setPosition:CGPointMake(0+despx,-rescaledHeight/2 - bottomLeft.contentSize.height*0.5 + despy)];
    left.scaleY = verticalScale;
    [left setPosition:CGPointMake(-rescaledWidth/2 - topLeft.contentSize.width/2 +despx, 0 + despy)];
    right.scaleY = verticalScale;
    [right setPosition:CGPointMake(rescaledWidth/2 + topRight.contentSize.width/2 +despx, 0 + despy)];
    [centre setPosition:CGPointMake(despx, despy)];
}

- (void)draw {
    [scale9Image draw];
}

@end
